﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using coffeeshop.Models;

namespace coffeeshop.Forms
{
    public partial class frmEditProductCate : Form
    {
        private ProductCategory theObject { get; set; }
        public Form MyParentForm { get; set; }
        public frmEditProductCate(ProductCategory _object)
        {
            InitializeComponent();

            if (_object != null)
            {
                theObject = _object;
                LoadObjectInfo();
            }
        }

        private void LoadObjectInfo()
        {
            if (theObject != null)
            {
                txtName.Text = theObject.Name;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!InputsValidated())
                return;

            if (theObject == null)
                theObject = new ProductCategory();
            theObject.Name = txtName.Text.Trim();

            if (theObject.Save())
            {
                this.Close();
                frmProductCategoryList listForm = (frmProductCategoryList)MyParentForm;
                listForm.ReloadList();
            }
            else
            {
                MessageBox.Show("Có lỗi khi lưu mặt hàng");
                return;
            }
        }

        private bool InputsValidated()
        {
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                MessageBox.Show("Cần điền tên mặt hàng");
                return false;
            }

            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
